// 1.typeof：使用率最多
function printID(id: number | string) {
    if (typeof id === "string") {
        console.log(id.length)
    } else {
        console.log(id)
    }
}

// 2.方向的类型判断
type Direction = "left" | "right" | "up" | "down"
function switchDirection(direction: Direction) {
    if (direction === "left") {
        console.log(direction, "角色向左移动")
    } else if (direction == "right") {
        console.log(direction)
    }
}

// 3.传入日期，打印日期
function printDate(date: string | Date) {
    if (date instanceof Date) {
        console.log(date.getTime())
    } else {
        console.log(date)
    }

    // if (typeof date === "string") {
    //     console.log(date)
    // } else {
    //     console.log(date.getTime())
    // }
}

// 4.判断是否有某一个属性
interface ISwim {
    swim: () => void
}

interface IRun {
    run: () => void
}

function move(animal: ISwim | IRun) {
    if ("swim" in animal) {
        animal.swim()
    } else if ("run" in animal) {
        animal.run()
    }
}

const fish: ISwim = {
    swim: function () {},
}

const dog: IRun = {
    run: function () {},
}

move(fish)
move(dog)

const info = {}
